package com.wx.service.impl;

import com.google.common.collect.Lists;
import com.wx.domain.dto.RoleDto;
import com.wx.domain.dto.UserBindRoleDto;
import com.wx.entity.SysAuthority;
import com.wx.entity.SysRole;
import com.wx.entity.SysUser;
import com.wx.entity.SysUserRole;
import com.wx.exception.BusinessException;
import com.wx.mapper.SysUserRoleMapper;
import com.wx.service.SysAuthorityService;
import com.wx.service.SysUserRoleService;
import com.wx.service.SysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * <p>Title:	  UserRoleServiceImpl <br/> </p>
 * <p>Description TODO <br/> </p>
 * <p>Company:    医百科技有限公司  <br/> </p>
 *
 * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>  <br/>
 * @Date 2018/1/16 14:45
 */
@Service
public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRole> implements SysUserRoleService {

    @Resource
    private SysAuthorityService authorityService;

    @Resource
    private SysUserRoleMapper userRoleMapper;

    @Resource
    private SysUserService userService;

    @Override
    public Collection<GrantedAuthority> loadUserAuthorities(String userId) {
        List<SysAuthority> ownAuthList = authorityService.getOwnAuthority(userId);
        List<GrantedAuthority> authList = Lists.newArrayList();
        for (SysAuthority sysAuthority : ownAuthList) {
            GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(sysAuthority.getUrl());
            authList.add(grantedAuthority);
        }
        return authList;
    }

    @Override
    public List<RoleDto> queryOwnRoles(SysUserRole query) {
        return userRoleMapper.queryOwnRoles(query);
    }

    @Override
    public List<SysUser> getBindUserByRoleId(String roleId) {
        return userRoleMapper.getBindUserByRoleId(roleId);
    }

    @Override
    public List<SysRole> getBindRoleListByUserId(String userId) {
        return userRoleMapper.getBindRoleListByUserId(userId);
    }

    @Override
    public void bindRole(UserBindRoleDto userBindRoleDto) {
        // 校验用户信息
        SysUser user = userService.selectByKey(userBindRoleDto.getUserId());
        if(user == null){
            throw  new BusinessException("查询用户信息不存在");
        }
        // 删除原来的关系
        userRoleMapper.deleteByUserId(userBindRoleDto.getUserId());

        if(CollectionUtils.isNotEmpty(userBindRoleDto.getRoleIdList())){

            List<SysUserRole> userRoles = this.bulidUserRoleInfo(userBindRoleDto);
            // 批量保存
            userRoleMapper.insertList(userRoles);
        }
    }

    @Override
    public void batchDeleteByRoleId(String roleId) {
        userRoleMapper.batchDeleteByRoleId(roleId);
    }


    @Override
    public List<SysUserRole> getBindRoleByRoleId(String roleId) {
        return userRoleMapper.getBindRoleByRoleId(roleId);
    }

    private List<SysUserRole> bulidUserRoleInfo(UserBindRoleDto userBindRoleDto){
        List<SysUserRole>  list = Lists.newArrayList();
        SysUserRole userRole = null;
        List<String> roleIdList = userBindRoleDto.getRoleIdList();
        for (String roleId : roleIdList) {
            userRole = new SysUserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(userBindRoleDto.getUserId());
            list.add(userRole);
        }
        return list;
    }
}
